//
//  YFHomeBannerView.m
//  YFListenStock
//
//  Created by funplus on 2020/12/13.
//  Copyright © 2020 ChinaRainbow-iOS. All rights reserved.
//

#import "YFHomeBannerView.h"

#import "YFBaseWebViewController.h"
#import "YFDetailVCRouteTool.h"
#import "YFBaseWebViewController.h"

@implementation YFHomeBannerView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}
-(instancetype)init
{
    if (self = [super init]) {
        [self setUp];
    }
    return self;
}
- (void)setUp
{
    [self addPagerView];
    [self addPageControl];
    UIView *view = [[UIView alloc]init];
    [self addSubview:view];
    view.backgroundColor = HexColor(0xF3F3F3);
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(6);
    }];
}
- (void)addPagerView {
    TYCyclePagerView *pagerView = [[TYCyclePagerView alloc]init];
    pagerView.isInfiniteLoop = YES;
    pagerView.autoScrollInterval = 3.0;
    pagerView.dataSource = self;
    pagerView.delegate = self;
    // registerClass or registerNib
    [pagerView registerClass:[TYCyclePagerViewCell class] forCellWithReuseIdentifier:@"cellId"];
    [self addSubview:pagerView];
    
    [self performSelector:@selector(updateType) withObject:nil afterDelay:0.1];
    _pagerView = pagerView;
}
-(void)updateType{
//    _pagerView.layout.layoutType = TYCyclePagerTransformLayoutLinear;
    _pagerView.layout.layoutType = TYCyclePagerTransformLayoutNormal;
    [_pagerView setNeedUpdateLayout];
}

- (void)addPageControl {
    TYPageControl *pageControl = [[TYPageControl alloc]init];
    //pageControl.numberOfPages = _datas.count;
    pageControl.currentPageIndicatorSize = CGSizeMake(12, 6);
    pageControl.pageIndicatorSize = CGSizeMake(6, 6);
    pageControl.currentPageIndicatorTintColor = HexColor(0xBFC5CE);
//    pageControl.pageIndicatorTintColor = HexColor(0xCFD6DF);
    pageControl.pageIndicatorTintColor = HexColor(0xFFFFFF);

    //    pageControl.pageIndicatorImage = [UIImage imageNamed:@"Dot"];
    //    pageControl.currentPageIndicatorImage = [UIImage imageNamed:@"DotSelected"];
    //    pageControl.contentInset = UIEdgeInsetsMake(0, 20, 0, 20);
    //    pageControl.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    //    pageControl.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    //    [pageControl addTarget:self action:@selector(pageControlValueChangeAction:) forControlEvents:UIControlEventValueChanged];
    [_pagerView addSubview:pageControl];
    _pageControl = pageControl;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _pagerView.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), YFHomeBannerCell_height );
    _pageControl.frame = CGRectMake(0, CGRectGetHeight(_pagerView.frame) -28, CGRectGetWidth(_pagerView.frame), 4);
}

-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    _pageControl.numberOfPages = dataArray.count;
    [_pagerView reloadData];
    [self performSelector:@selector(updateType) withObject:nil afterDelay:0.1];
}
#pragma mark - TYCyclePagerViewDataSource

- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    return self.dataArray.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    TYCyclePagerViewCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndex:index];
    YFBannerModel *model = [self.dataArray safeObjectWithIndex:index];
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.url] placeholderImage:SETIMAGE(@"logo")];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
//    layout.itemSize = CGSizeMake(CGRectGetWidth(pageView.frame)-36, CGRectGetHeight(pageView.frame));
//    layout.itemSpacing = 7;
    layout.itemSize = CGSizeMake(CGRectGetWidth(pageView.frame), CGRectGetHeight(pageView.frame));
    layout.itemSpacing = 0;
    layout.itemHorizontalCenter = YES;
    return layout;
}

- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    _pageControl.currentPage = toIndex;
    //[_pageControl setCurrentPage:newIndex animate:YES];
//    NSLog(@"%ld ->  %ld",fromIndex,toIndex);
}
- (void)pageControlValueChangeAction:(TYPageControl *)sender {
    NSLog(@"pageControlValueChangeAction: %ld",(long)sender.currentPage);
}
- (void)pagerView:(TYCyclePagerView *)pageView didSelectedItemCell:(nonnull __kindof UICollectionViewCell *)cell atIndex:(NSInteger)index{
    YFBannerModel *model = [self.dataArray safeObjectWithIndex:index];
    YFBaseViewController *vc = (YFBaseViewController *)self.nearestViewController;
    [model clickWithVC:vc];
}

@end

